import pytest
import requests

from api import log
from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from tool import html_parser, common_assert, read_json


class TestTender:
    def setup(self):
        self.session = requests.session()
        self.api = ApiTender(self.session)
        r = ApiRegisterLogin(self.session).api_login()
        log.info("登录结果为：{}".format(r.json()))

    def teardown(self):
        self.session.close()

    @pytest.mark.parametrize("amount,expect_text", read_json("tender.json", "tender"))
    def test01_tender(self, amount, expect_text):
        try:
            result = self.api.api_tender(amount)
            log.info("后台投资接口结果为：{}".format(result.json()))
            if expect_text == "OK":
                # 三方投资
                data = html_parser(result)
                result = self.session.post(url=data[0], data=data[1])
                log.info("三方充值接口执行结果为：{}".format(result.text))
            common_assert(result, expect_text=expect_text)
        except Exception as e:
            log.error(e)
            raise
